//
//  BLBigImageViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/22.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLBigImageViewController.h"
#import "BLTopic.h"
#import "UIImageView+WebCache.h"
#import "SVProgressHUD.h"
#import <YYWebImage.h>

@interface BLBigImageViewController ()<UIScrollViewDelegate>

- (IBAction)back:(id)sender;

@property (weak, nonatomic) IBOutlet UIScrollView *myScrollerView;

@property (nonatomic, weak)YYAnimatedImageView *bigImageView;

- (IBAction)saveButton:(id)sender;
@end

@implementation BLBigImageViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    CGFloat imageW = self.topic.width;
    CGFloat imageH = self.topic.height;
    CGFloat myImageW = mainW;
    CGFloat myImageH = imageH*mainW/imageW;
    YYAnimatedImageView *bigImageView = [YYAnimatedImageView new];
    bigImageView.bounds = CGRectMake(0, 0, myImageW, myImageH);
    
    [self.myScrollerView addSubview:bigImageView];
    
    self.myScrollerView.contentSize = CGSizeMake(0, myImageH);
    
    if (myImageH > mainH) {
        bigImageView.frame = CGRectMake(0, 0, myImageW, myImageH);
    }else{
        bigImageView.center = CGPointMake(mainW * 0.5, mainH * 0.5);
        
    }
    
   // [bigImageView sd_setImageWithURL:[NSURL URLWithString:self.topic.image2]];
    bigImageView.yy_imageURL = [NSURL URLWithString:self.topic.image2];
    bigImageView.userInteractionEnabled = YES;
    [bigImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back:)]];
    
    self.bigImageView = bigImageView;
    
    self.myScrollerView.delegate = self;
    self.myScrollerView.minimumZoomScale =1.0;
    self.myScrollerView.maximumZoomScale = 1.4;
    self.myScrollerView.frame = CGRectMake(0, 0, mainW, mainH);
    self.myScrollerView.autoresizingMask = UIViewAutoresizingNone;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)saveButton:(id)sender {
    UIImageWriteToSavedPhotosAlbum(self.bigImageView.image, self, @selector(saveSeccess:didFinishSavingWithError:contextInfo:), nil);
}

- (void)saveSeccess:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [SVProgressHUD showSuccessWithStatus:@"保存成功!"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"保存失败!"];
    }
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.bigImageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    if (scrollView.zoomScale >1 ) {
        scrollView.bouncesZoom = YES;
    }else{
        scrollView.bouncesZoom = NO;

    }
}
@end
